<?php
namespace app\house\controller;
use app\ApiRest;
use app\AdminRest;
use think\App;
use think\facade\Db;
use think\Request;
use app\house\model\IndexAppointment;
use app\house\model\IndexCollect;
use app\shop\model\IndexCardCount;


class IndexAppHouse extends ApiRest{
    protected $model;
    protected $collect_model;
    protected $count_model;

    public function __construct(App $app) {
        parent::__construct($app);
        $this->model        = new IndexAppointment();
        $this->collect_model= new IndexCollect();
        $this->count_model  = new IndexCardCount();

    }
    /**
     *
     * 我的房源
     */
    public function myHouse(){
        $input = $this->_input;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        $dis[] = ['a.uid','=',$this->getUserId()];
        $dis[] = ['b.uniacid','=',$this->_uniacid];
        $dis[] = ['b.status','=',1];
        if(!empty($input['type'])&&$input['type']==1){
            $dis[] = ['a.status','=',1];
            $data = $this->model->appList($dis);
        }else{
            $data = $this->collect_model->collectList($dis);
        }
        return $this->success($data);
    }


    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 添加|删除收藏
     */
    public function addCollect(){
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $input['uid']     = $this->getUserId();
        $collect = $this->collect_model->collectInfo($input);
        if(!empty($collect)){
            $res = $this->collect_model->collectDel(['id'=>$collect['id']]);
        }else{
            $res = $this->collect_model->collectAdd($input);
        }
        return $this->success($res);
    }
    /**
     *
     * 预约房产
     */
    public function addApp(){
        $input = $this->_input;
        $uid   = $this->getUserId();
        $input['date'] = strtotime($input['date']);
        $yesterday = strtotime(date('Y-m-d',time()));
        if($yesterday>$input['date']){
            return $this->error('不能预约今天之前的时间');
        }
        $yester= strtotime(date('Y-m-d',$input['date']));
        $today = $yester+86400;
        $dis[] = ['house_id','=',$input['house_id']];
        $dis[] = ['uid','=',$uid];
        $dis[] = ['date','between',"$yester,$today"];
        $house = $this->model->appInfo($dis);
        if($house){
            return $this->error('您已经预约了该天看该房源,去看看别的房源~');
        }
        $avatar = Db::name('longbing_card_user')->where(['id'=>$uid])->value('avatarUrl');
        $avatar = $avatar ?  $avatar  :'https://tongchengxiaodian.oss-cn-beijing.aliyuncs.com/qiyetest/attachment/upload/3/picture/2019-01-03/5c2db450c58c4.png';
        $input['uniacid'] = $this->_uniacid;
        $input['uid']     = $uid;
        $input['avatar']  = $avatar;
        $res = $this->model->appAdd($input);
        if($uid!=$input['to_uid']){
            $data=[
                'uniacid'=>$this->_uniacid,
                'user_id'=>$uid,
                'to_uid' =>$input['to_uid'],
                'type'   =>8,
                'sign'   =>'praise',
                'target' =>$input['house_id'],
            ];
            $insert_id = $this->count_model->countAdd($data);
            $push_data = array(
                'action'  => 'longbingSendWxServiceNotice',
                'event'   => 'longbingSendWxServiceNotice',
                'count_id'=> $insert_id
            );
            publisher(json_encode($push_data ,true));
        }
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-11-01 13:53
     * @return \think\Response
     * descption:编辑预约|添加备注
     */
    public function appText(){
        $input = $this->_input;
        $res   = $this->model->appUpdate(['id'=>$input['id']],$input);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-11-01 14:03
     * @return \think\Response
     * descption:编辑收藏|添加备注
     */
    public function collectText(){
        $input = $this->_input;
        $res   = $this->collect_model->collectUpdate(['id'=>$input['id']],$input);
        return $this->success($res);

    }









}
